package cn.com.git.admin.server.ctrler;

import cn.com.git.admin.server.api.feign.upms.SystemFeignService;
import cn.com.git.admin.server.service.WebSocketService;
import cn.com.git.admin.service.dto.SysResourceDetailDTO;
import cn.com.git.admin.service.dto.SysResourceListDTO;
import cn.com.git.admin.service.dto.SysResourceSaveDTO;
import cn.com.git.admin.service.query.SysResourceListQuery;
import cn.com.git.common.base.BaseController;
import cn.com.git.common.model.DataTree;
import cn.com.git.common.model.ResultMsg;
import cn.com.git.common.model.ResultPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.List;

/**
 * @author zhangzhenfu
 * @Description
 * @date 2020/3/1 0001 12:26
 */
@Controller
@RequestMapping("/sys_resource")
public class SysResourceCtrler extends BaseController {

    @Autowired
    private SystemFeignService systemFeignService;

    @Autowired
    private WebSocketService webSocketService;


    @RequestMapping(value = "/index",method = RequestMethod.GET)
    public ModelAndView index(){
        ModelAndView mv = new ModelAndView("sys_resource/index");
        webSocketService.sendMessage("资源主页");
        return mv ;
    }

    @RequestMapping(value = "/list/page", method = RequestMethod.POST)
    @ResponseBody
    public ResultPage<SysResourceListDTO> listPage(SysResourceListQuery query) {
        ResultPage<SysResourceListDTO> resultPage = systemFeignService.sysResourceListPage(query);
        webSocketService.sendMessage("资源列表查询");
        return resultPage;
    }

    @RequestMapping(value = "/list/tree", method = RequestMethod.POST)
    @ResponseBody
    public List<DataTree> listTree(SysResourceListQuery query) {
        ResultMsg<List<DataTree>> resultMsg = systemFeignService.sysResourceListTree(query);
        webSocketService.sendMessage("资源列表（树）查询");
        return resultMsg.getData();
    }

    @RequestMapping(value = "/add", method = RequestMethod.GET)
    public ModelAndView add() {
        ModelAndView mv = new ModelAndView("sys_resource/add");
        webSocketService.sendMessage("资源新增页面");
        return mv ;
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<SysResourceSaveDTO> add(@RequestBody SysResourceSaveDTO form) {
        ResultMsg<SysResourceSaveDTO> resultMsg = systemFeignService.sysResourceAdd(form);
        webSocketService.sendMessage("资源新增");
        return resultMsg;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public ResultMsg<SysResourceSaveDTO> edit(@RequestBody SysResourceSaveDTO form) {
        ResultMsg<SysResourceSaveDTO> resultMsg = systemFeignService.sysResourceEdit(form);
        webSocketService.sendMessage("资源编辑页面");
        return resultMsg;
    }

    @RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
    public ModelAndView edit(@PathVariable("id") Integer id) {
        ModelAndView mv = new ModelAndView("sys_resource/edit");
        ResultMsg<SysResourceDetailDTO> resultMsg = systemFeignService.sysResourceFind(id);
        resultMsg.getData().getParent().getIcon();
        mv.addObject("dto",resultMsg.getData());
        webSocketService.sendMessage("资源编辑");
        return mv ;
    }

    @RequestMapping(value = "/delete/{id}", method = RequestMethod.GET)
    @ResponseBody
    public ResultMsg delete(@PathVariable("id") Integer id) {
        ResultMsg resultMsg = systemFeignService.sysResourceDelete(id);
        return resultMsg;
    }
}
